

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.io.Serializable;



public class PacoteUDP implements Comparable, Serializable {

    private int packetType;
    private int packetID;
    private byte[] packetData;
    private int length;


    public PacoteUDP (){
        packetType=0;
        packetID=0;  
    }
    
    public PacoteUDP(int type, int id, byte[] d){
        packetType=type;
        packetID=id;
        packetData = d;
    }
    
    public PacoteUDP(int type,int id,byte[] data, int tamanho){
        packetType=type;
        packetID=id;
        packetData=data;
        length = tamanho;

    }

    public PacoteUDP(PacoteUDP pack){
        packetType=pack.getType();
        packetID=pack.getID();
        packetData=pack.getData();
        length = pack.getLength();
    }


    public int getType() {return packetType;}
    public int getID() {return packetID;}
    public byte[] getData() {return packetData;}
    private int getLength() {return length;}


    public void setType(int tipo) {packetType = tipo;}
    public void setID(int iD) {packetID = iD;}
    public void setData(byte[] data) {packetData = data;}

    private void setLength(int tamanho) {length= tamanho;}


    public static PacoteUDP bytesToPacketUDP (byte[] bytes){
      Object obj = null;
      try {
        ByteArrayInputStream byteArray = new ByteArrayInputStream (bytes);
        ObjectInputStream objInputStream = new ObjectInputStream (byteArray);
        obj = objInputStream.readObject();
      }
      catch (Exception ex) {
          System.out.println("###Erro em bytesToPacketUDP " + ex.getMessage());
      }
      return (PacoteUDP)obj;
    }


     public static byte[] divideEmBytes(Object o) throws IOException{
        ByteArrayOutputStream bos = new ByteArrayOutputStream() ;
        ObjectOutput out = new ObjectOutputStream(bos) ;
        out.writeObject(o);
        out.close();
        return bos.toByteArray();
    }
     

    public int compareTo(Object obj) {

        PacoteUDP p = (PacoteUDP) obj;

        if(this.getID() < p.getID()){return -1;}
        if(this.getID() > p.getID()){return 1;}

        else return 0;
    }

}
